package com.weike.wk_android.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.trello.rxlifecycle3.components.RxActivity;
import com.weike.wk_android.R;
import com.weike.wk_android.contract.LoginContract;
import com.weike.wk_android.impl.ETPhoneListener;
import com.weike.wk_android.model.UserInfo;
import com.weike.wk_android.presenter.LoginPresenter;
import com.weike.wk_android.utils.Utils;
import com.weike.wk_android.widget.toast.MyToast;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends RxActivity implements LoginContract.View {
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_password)
    EditText loginPassword;

    private LoginContract.Presenter presenter;
    private Intent intent;

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        new LoginPresenter(this, this);

        // 如果需要presenter初始化,调用此方法
        presenter.start();
        initListener();
    }

    /**
     * 手机号绑定事件
     */
    private void initListener() {
        loginPhone.addTextChangedListener(new ETPhoneListener(loginPhone));
    }

    @Override
    public void showLoading() {
        Toast.makeText(this, "加载中...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoginInfo(UserInfo userInfo) {
        // 做一些其他处理，显示头像，昵称...
        MyToast.showToast(this, userInfo.toString());

        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    public void dismissLoading() {
        Toast.makeText(this, "加载结束。", Toast.LENGTH_SHORT).show();
    }


    @OnClick({R.id.login_close, R.id.login, R.id.login_qq,
            R.id.login_wechat, R.id.register, R.id.fotget_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_close:
                close();
                break;
            case R.id.login:
                login();
                break;
            case R.id.login_qq:
                gotoQQ();
                break;
            case R.id.login_wechat:
                gotoWechat();
                break;
            case R.id.register:
                gotoRegister();
                break;
            case R.id.fotget_password:
                gotoFotgetPassword();
                break;
        }
    }

    /**
     * 跳转忘记密码页面
     */
    private void gotoFotgetPassword() {
        intent = new Intent(this, ForgetPasswordActivity.class);
        startActivity(intent);
    }

    /**
     * 跳转注册页面
     */
    private void gotoRegister() {
        intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }

    /**
     * 绑定微信
     */
    private void gotoWechat() {
    }

    /**
     * 绑定QQ
     */
    private void gotoQQ() {
    }

    /**
     * 关闭当前页面
     */
    private void close() {
        finish();
    }

    /**
     * 登录
     */
    private void login() {
        String username = loginPhone.getText().toString();
        String password = loginPassword.getText().toString();

        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            MyToast.showToast(this, Utils.getString(R.string.login_nan));
            return;
        }else if(Utils.regPatternsPassword(this, password)){
            return;
        }

        startActivity(new Intent(this, MainActivity.class));

//        presenter.login(username, password);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearUp();
    }

    private void clearUp() {
        intent = null;
        presenter = null;
    }


}
